package com.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateUtil {
	private static final SessionFactory sessionFactory;
	
	static{
		//5.0以上的版本使用以下的方法获取SessionFactory
		StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure().build();
		sessionFactory=new MetadataSources(ssr).buildMetadata().buildSessionFactory();
		//4.3版本的hibernate用以下的方法获取SessionFactory
		//Configuration cfg=new Configuration().configure("../../com/xml/hibernate.cfg.xml");
		//ServiceRegistry sr=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
		//sessionFactory=cfg.buildSessionFactory(sr);
		System.out.println(sessionFactory);
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	public static void closeSessionFactory(){
		getSessionFactory().close();
	}
}
